package com.cashmama.market.api.application.controller.home;

import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.application.annos.VisitorAccessible;
import com.cashmama.market.api.application.controller.home.request.ProductClickRequest;
import com.cashmama.market.api.application.controller.home.request.ProductDetailRequest;
import com.cashmama.market.api.application.controller.home.request.ProductListRequest;
import com.cashmama.market.api.application.controller.home.response.BottomMenuVO;
import com.cashmama.market.api.application.controller.home.response.ConfigInfoResponse;
import com.cashmama.market.api.application.controller.home.response.CreditHomeResponse;
import com.cashmama.market.api.application.controller.home.response.ProductListResponse;
import com.cashmama.market.api.domain.service.ProductService;
import com.cashmama.market.api.domain.utils.AppUtils;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.home.CategoryVO;
import com.cashmama.market.api.domain.vo.home.HomeListVo;
import com.cashmama.market.api.domain.vo.home.HomeVO;
import com.cashmama.market.api.domain.vo.product.ProductDetailVO;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.enums.PackageNameEnum;
import com.cashmama.market.api.infras.enums.product.BottomMenuCashMallEnum;
import com.cashmama.market.api.infras.enums.product.BottomMenuEnum;
import com.cashmama.market.api.infras.handler.ApiResponse;
import com.cashmama.market.api.infras.utils.StringUtils;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.xjd.user.client.UserService;
import com.fqgj.xjd.user.client.response.UserProductPromotionLogRes;
import com.google.common.collect.Lists;
import com.qianli.cashmama.product.facade.result.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @program: market-api
 * @description 首页
 * @author: frank
 * @create: 2019-12-19 21:16
 **/
@RestController
@RequestMapping("/product")
public class HomeController {

    @Autowired
    private ProductService productService;
    @Autowired
    private AppUtils appUtils;
    @Autowired
    private ApolloConfigUtil apolloConfigUtil;

    @Autowired
    private UserService userService;

    /**
     * 首页信息
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/home", method = RequestMethod.POST)
    public ApiResponse<HomeVO> home() {
        HomeVO homeVO = productService.getHomeInfo();
        return new ApiResponse<>(homeVO);
    }

    /**
     * 首页信息
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/home/list", method = RequestMethod.POST)
    public ApiResponse<HomeListVo> homeList() {
        HomeListVo homeVO = new HomeListVo();
        homeVO.setCreditAmount("₹6000");
        homeVO.setBottomTip("Boost your credit by repaying your loan on time");
        ArrayList<HomeListVo.ProductInfo> list = Lists.newArrayList();
        HomeListVo.ProductInfo productInfo = new HomeListVo.ProductInfo();
        productInfo.setDescription("Repayment in 7 days");
        productInfo.setButtonText("Repay");
        productInfo.setIcon("https://dc-oss2.oss-ap-south-1.aliyuncs.com/app/rupeemama/ic_product_three.png");
        productInfo.setLocked(false);
        productInfo.setTitle("₹3000");
        productInfo.setJumpUrl("URL/orderdetail?tradeNo=231242620838348010DC");
        list.add(productInfo);
        homeVO.setList(list);
        return new ApiResponse<>(homeVO);
    }

    /**
     * 首页产品点击
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/home/product/click", method = RequestMethod.POST)
    public ApiResponse<String> homeProductClick(@RequestBody @Valid ProductClickRequest productClickRequest) {
//        productService.getHomeProductClick(productClickRequest.getCategoryCode());
        try {
            UserProductPromotionLogRes userProductPromotionLogRes = new UserProductPromotionLogRes()
                    .setActionFrom(RequestLocalInfo.getCurrentUser().getUserCode())
                    .setActionTarget(productClickRequest.getCategoryCode())
                    .setActionType("homepage-click");
            userService.addPromotion(userProductPromotionLogRes);
        } catch (Exception e) {

        }
        return new ApiResponse<>();
    }


    /**
     * 首页底部菜单引导
     *
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/bottom/menu/list", method = RequestMethod.POST)
    public ApiResponse<List<BottomMenuVO>> bottomMenuList() {
        List<BottomMenuVO> list = Lists.newArrayList();
        List<MenuVO> menus = Lists.newArrayList();
        String menuStr = appUtils.getMenuListUrl(PackageNameEnum.getLowercaseAppName(RequestLocalInfo.getRequestBasicInfo().getPackageName()));
        if (StringUtils.isNotEmpty(menuStr)) {
            menus = JSONObject.parseArray(appUtils.getMenuListUrl(PackageNameEnum.getLowercaseAppName(RequestLocalInfo.getRequestBasicInfo().getPackageName())), MenuVO.class);
        }
        if (CollectionUtils.isNotEmpty(menus)) {
            for (MenuVO menu : menus) {
                if (!appUtils.getAppConfig().getProductListSwitch()) {
                    if (menu.getTitle().equalsIgnoreCase(BottomMenuEnum.PRODUCT_LIST.getTitle())) {
                        continue;
                    }
                }
                BottomMenuVO bottomMenuVO = new BottomMenuVO();
                bottomMenuVO.setTitle(menu.getTitle());
                bottomMenuVO.setImgUrl(menu.getImgUrl());
                bottomMenuVO.setImgCheckedUrl(menu.getImgCheckedUrl());
                bottomMenuVO.setJumpUrl(menu.getJumpUrl());
                bottomMenuVO.setNeedLogin(Boolean.parseBoolean(menu.getNeedLogin()));
                if (Objects.equals(BottomMenuEnum.HOME.getTitle(), menu.getTitle())) {
                    bottomMenuVO.setJumpUrl(menu.getJumpUrl());
                }
                list.add(bottomMenuVO);
            }
        } else {
            if (RequestLocalInfo.getRequestBasicInfo().getPackageName().equals(PackageNameEnum.CASHMall.getPackageName()) || RequestLocalInfo.getRequestBasicInfo().getPackageName().equals(PackageNameEnum.RUPEEMAMA.getPackageName())) {
                BottomMenuCashMallEnum[] values = BottomMenuCashMallEnum.values();
                for (BottomMenuCashMallEnum value : values) {
                    BottomMenuVO bottomMenuVO = new BottomMenuVO();
                    bottomMenuVO.setTitle(value.getTitle());
                    bottomMenuVO.setImgUrl(value.getImgUrl());
                    bottomMenuVO.setImgCheckedUrl(value.getImgCheckedUrl());
                    bottomMenuVO.setJumpUrl(value.getJumpUrl());
                    bottomMenuVO.setNeedLogin(value.getNeedLogin());
                    list.add(bottomMenuVO);
                }
            } else {
                BottomMenuEnum[] values = BottomMenuEnum.values();
                for (BottomMenuEnum value : values) {
                    if (!appUtils.getAppConfig().getProductListSwitch()) {
                        if (value.equals(BottomMenuEnum.PRODUCT_LIST) || value.equals(BottomMenuEnum.SMS_REPORT)) {
                            continue;
                        }
                    }
                    BottomMenuVO bottomMenuVO = new BottomMenuVO();
                    bottomMenuVO.setTitle(value.getTitle());
                    bottomMenuVO.setImgUrl(value.getImgUrl());
                    bottomMenuVO.setImgCheckedUrl(value.getImgCheckedUrl());
                    bottomMenuVO.setJumpUrl(value.getJumpUrl());
                    bottomMenuVO.setNeedLogin(value.getNeedLogin());
                    if (Objects.equals(BottomMenuEnum.HOME, value)) {
                        bottomMenuVO.setJumpUrl(value.getJumpUrl());
                    }
                    list.add(bottomMenuVO);
                }
            }

        }
        return new ApiResponse<>(list);
    }

    /**
     * 类目详情接口
     */
    @ParamsValidate
    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    public ApiResponse<ProductDetailVO> detail(@RequestBody @Valid ProductDetailRequest productDetailRequest) {
        ProductDetailVO productDetailInfo = productService.getProductDetailInfo(productDetailRequest.getMerchantCode());
        return new ApiResponse<>(productDetailInfo);
    }

    /**
     * 贷超产品列表
     *
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<ProductListResponse> productList(@RequestBody @Valid ProductListRequest productListRequest) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        List<CategoryVO> categoryList = productService.getCategoryList(userCode);
        return new ApiResponse<>(new ProductListResponse(categoryList));
    }

    /**
     * 授信首页
     *
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/credit/home", method = RequestMethod.POST)
    public ApiResponse<CreditHomeResponse> creditHome() {
        return new ApiResponse<>(new CreditHomeResponse(productService.creditHome()));
    }

    /**
     * 配置信息
     *
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/info/config", method = RequestMethod.POST)
    public ApiResponse<ConfigInfoResponse> infoConfig() {
        return new ApiResponse<>(new ConfigInfoResponse(apolloConfigUtil.needALGO360Verify()));
    }


}
